/**
 ****************************************************************************************************
 * @file        lvgl_ui.c
 * @author      
 * @version     V1.0
 * @date        2024-01-01
 * @brief       电机控制界面实现
 * @license     
 ****************************************************************************************************
 */

#include "lvgl_ui.h"
#include "./BSP/CAN/can.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// 声明自定义字体
LV_FONT_DECLARE(my_font_24)

// 控件对象定义
lv_obj_t *speed_meter = NULL;              // 速度仪表盘
lv_obj_t *speed_label = NULL;              // 速度数值显示
// lv_obj_t *preset_speed_label = NULL;       // 预设速度数值显示
lv_obj_t *preset_speed_input = NULL;       // 预设速度输入框
lv_obj_t *speed_slider = NULL;             // 速度滑块
lv_obj_t *start_btn = NULL;                // 启动按钮
lv_obj_t *stop_btn = NULL;                 // 停止按钮
lv_obj_t *reverse_btn = NULL;              // 反转按钮
lv_obj_t *zero_btn = NULL;                 // 归零按钮

// 数字键盘弹窗对象定义
lv_obj_t *keyboard_popup = NULL;           // 键盘弹窗容器
lv_obj_t *keyboard_bg = NULL;              // 弹窗背景
lv_obj_t *keyboard = NULL;                 // 数字键盘
lv_obj_t *keyboard_input_display = NULL;   // 键盘输入显示区域

// 电机状态变量
int32_t current_speed = 0;                 // 当前实际速度
int32_t preset_speed = 0;                  // 预设速度
uint8_t motor_running = 0;                 // 电机运行状态
uint8_t motor_direction = 0;               // 电机方向 0:正转 1:反转

// 仪表盘指针
static lv_meter_indicator_t *speed_indicator = NULL;

// 键盘输入相关变量（现在直接使用textarea作为输入缓存）

// 获取屏幕宽高
#define scr_act_width() lv_obj_get_width(lv_scr_act())
#define scr_act_height() lv_obj_get_height(lv_scr_act())

/**
 * @brief  启动按钮事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void start_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        motor_running = 1;
        printf("Motor Start - Preset Speed: %d RPM\r\n", (int)preset_speed);
        
        // 发送启动指令 + 预设速度 (ID: 0x102)
        send_can_command(0x102, preset_speed);
        
        // // 更改按钮颜色状态
        // lv_obj_set_style_bg_color(start_btn, lv_color_hex(0x4CAF50), LV_STATE_DEFAULT);
        // lv_obj_set_style_bg_color(stop_btn, lv_color_hex(0xCCCCCC), LV_STATE_DEFAULT);
    }
}

/**
 * @brief  停止按钮事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void stop_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        motor_running = 0;
        current_speed = 0;
        printf("Motor Stop\r\n");
        
        // 发送停止指令 (ID: 0x101)
        send_can_command(0x101, 0);
        
        // // 更改按钮颜色状态
        // lv_obj_set_style_bg_color(stop_btn, lv_color_hex(0xF44336), LV_STATE_DEFAULT);
        // lv_obj_set_style_bg_color(start_btn, lv_color_hex(0xCCCCCC), LV_STATE_DEFAULT);
        
        // // 更新速度显示
        // update_motor_speed_display(0);
    }
}

/**
 * @brief  反转按钮事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void reverse_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        // 直接改变预设速度的符号
        preset_speed = -preset_speed;
        
        printf("Motor Reverse - New Speed: %d RPM\r\n", (int)preset_speed);
        
        // 更新界面显示和滑块
        update_preset_speed_display(preset_speed);
        lv_slider_set_value(speed_slider, preset_speed, LV_ANIM_ON);
        
    
    }
}

/**
 * @brief  归零按钮事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void zero_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        // 将预设速度设为0
        preset_speed = 0;
        
        printf("Speed Reset to Zero\r\n");
        
        // 更新界面显示和滑块
        update_preset_speed_display(preset_speed);
        lv_slider_set_value(speed_slider, preset_speed, LV_ANIM_ON);
    }
}

/**
 * @brief  减号按钮事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void minus_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        preset_speed -= 100;
        if (preset_speed < -3000)
        {
            preset_speed = -3000;
        }
        
        update_preset_speed_display(preset_speed);
        lv_slider_set_value(speed_slider, preset_speed, LV_ANIM_ON);
        printf("Preset Speed: %d RPM\r\n", (int)preset_speed);
    }
}

/**
 * @brief  加号按钮事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void plus_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        preset_speed += 100;
        if (preset_speed > 3000)
        {
            preset_speed = 3000;
        }
        
        update_preset_speed_display(preset_speed);
        lv_slider_set_value(speed_slider, preset_speed, LV_ANIM_ON);
        printf("Preset Speed: %d RPM\r\n", (int)preset_speed);
    }
}

/**
 * @brief  滑块事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void slider_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_VALUE_CHANGED)
    {
        preset_speed = lv_slider_get_value(speed_slider);
        update_preset_speed_display(preset_speed);
        // printf("Slider Speed: %d RPM\r\n", (int)preset_speed);
    }
}

/**
 * @brief  预设速度输入框点击事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void preset_input_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_CLICKED)
    {
        show_number_keyboard_popup();
        printf("Show Number Keyboard\r\n");
    }
}

/**
 * @brief  数字键盘事件回调
 * @param  e: 事件对象
 * @return 无
 */
static void keyboard_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *kb = lv_event_get_target(e);
    
    if (code == LV_EVENT_VALUE_CHANGED)
    {
        const char *txt = lv_textarea_get_text(keyboard_input_display);
        printf("Keyboard Input: %s\r\n", txt);
    }
    else if (code == LV_EVENT_READY)
    {
        // 当用户按下键盘上的√确认键时
        const char *txt = lv_textarea_get_text(keyboard_input_display);
        
        // 解析输入的数字
        int32_t input_value = 0;
        if (txt != NULL && strlen(txt) > 0)
        {
            input_value = atoi(txt);
            
            // 限制输入范围
            if (input_value > 3000)
            {
                input_value = 3000;
            }
            else if (input_value < -3000)
            {
                input_value = -3000;
            }
            
            // 更新预设速度
            preset_speed = input_value;
            update_preset_speed_display(preset_speed);
            lv_slider_set_value(speed_slider, preset_speed, LV_ANIM_ON);
            
            printf("Preset Speed Set: %d RPM\r\n", (int)preset_speed);
        }
        
        // 关闭弹窗
        close_number_keyboard_popup();
    }
    else if (code == LV_EVENT_CANCEL)
    {
        // 当用户按下键盘上特殊取消键时（图中红框的按键）
        printf("Cancel Input\r\n");
        // 关闭弹窗，不更新数值
        close_number_keyboard_popup();
    }
}


/**
 * @brief  创建速度仪表盘
 * @param  无
 * @return 无
 */
static void create_speed_meter(void)
{
    // 创建仪表盘
    speed_meter = lv_meter_create(lv_scr_act());
    lv_obj_set_size(speed_meter, 170, 170);
    lv_obj_set_pos(speed_meter, 20, 50);
    
    // 创建刻度
    lv_meter_scale_t *scale = lv_meter_add_scale(speed_meter);
    lv_meter_set_scale_ticks(speed_meter, scale, 21, 2, 10, lv_palette_main(LV_PALETTE_GREY));
    lv_meter_set_scale_major_ticks(speed_meter, scale, 5, 4, 15, lv_color_black(), 10);
    lv_meter_set_scale_range(speed_meter, scale, -3000, 3000, 270, 135);
    
    // 添加指针
    speed_indicator = lv_meter_add_needle_line(speed_meter, scale, 4, lv_palette_main(LV_PALETTE_RED), -10);
    
    // 创建"实际速度"标题
    lv_obj_t *title_label = lv_label_create(lv_scr_act());
    lv_label_set_text(title_label, "实际速度");
    lv_obj_set_style_text_font(title_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_set_pos(title_label, 50, 10);
    
    // 创建RPM标签
    lv_obj_t *rpm_label = lv_label_create(lv_scr_act());
    lv_label_set_text(rpm_label, "RPM");
    lv_obj_set_style_text_font(rpm_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_set_pos(rpm_label, 10, 255);
    
    // 创建速度数值显示
    speed_label = lv_label_create(lv_scr_act());
    lv_label_set_text(speed_label, "0");
    lv_obj_set_style_text_font(speed_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(speed_label, lv_color_white(), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(speed_label, LV_OPA_COVER, LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(speed_label, 1, LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(speed_label, lv_color_black(), LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(speed_label, 5, LV_STATE_DEFAULT);
    lv_obj_set_pos(speed_label, 70, 250);
    lv_obj_set_size(speed_label, 100, 40);
    lv_obj_set_style_text_align(speed_label, LV_TEXT_ALIGN_CENTER, LV_STATE_DEFAULT);
}

/**
 * @brief  创建控制按钮组
 * @param  无
 * @return 无
 */
static void create_control_buttons(void)
{
    // 启动按钮
    start_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(start_btn, 70, 40);
    lv_obj_set_pos(start_btn, 220, 50);
    lv_obj_set_style_bg_color(start_btn, lv_color_hex(0x4CAF50), LV_STATE_DEFAULT);
    lv_obj_add_event_cb(start_btn, start_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *start_label = lv_label_create(start_btn);
    lv_label_set_text(start_label, "启动");
    lv_obj_set_style_text_font(start_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_center(start_label);
    
    // 停止按钮
    stop_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(stop_btn, 70, 40);
    lv_obj_set_pos(stop_btn, 300, 50);
    lv_obj_set_style_bg_color(stop_btn, lv_color_hex(0xF44336), LV_STATE_DEFAULT);
    lv_obj_add_event_cb(stop_btn, stop_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *stop_label = lv_label_create(stop_btn);
    lv_label_set_text(stop_label, "停止");
    lv_obj_set_style_text_font(stop_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_center(stop_label);
    
    // 反转按钮
    reverse_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(reverse_btn, 70, 40);
    lv_obj_set_pos(reverse_btn, 380, 20);  // 向上移动20像素
    lv_obj_set_style_bg_color(reverse_btn, lv_color_hex(0x2196F3), LV_STATE_DEFAULT);
    lv_obj_add_event_cb(reverse_btn, reverse_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *reverse_label = lv_label_create(reverse_btn);
    lv_label_set_text(reverse_label, "反转");
    lv_obj_set_style_text_font(reverse_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_center(reverse_label);

    // 归零按钮
    zero_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(zero_btn, 70, 40);
    lv_obj_set_pos(zero_btn, 380, 80);  // 在反转按钮下方30像素 (30+30+20=80)
    lv_obj_set_style_bg_color(zero_btn, lv_color_hex(0xFFC107), LV_STATE_DEFAULT); // 黄色
    lv_obj_add_event_cb(zero_btn, zero_btn_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *zero_label = lv_label_create(zero_btn);
    lv_label_set_text(zero_label, "归零");
    lv_obj_set_style_text_font(zero_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_center(zero_label);
}

/**
 * @brief  创建预设速度控制组
 * @param  无
 * @return 无
 */
static void create_preset_speed_control(void)
{
    // 预设速度标题
    lv_obj_t *preset_title = lv_label_create(lv_scr_act());
    lv_label_set_text(preset_title, "预设速度");
    lv_obj_set_style_text_font(preset_title, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_set_pos(preset_title, 280, 120);
    
    // 减号按钮
    lv_obj_t *minus_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(minus_btn, 35, 35);
    lv_obj_set_pos(minus_btn, 240, 155);
    lv_obj_add_event_cb(minus_btn, minus_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *minus_label = lv_label_create(minus_btn);
    lv_label_set_text(minus_label, "-");
    lv_obj_set_style_text_font(minus_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_center(minus_label);
    
    // 预设速度输入框
    preset_speed_input = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(preset_speed_input, 90, 35);
    lv_obj_set_pos(preset_speed_input, 285, 155);
    lv_textarea_set_text(preset_speed_input, "0");
    lv_textarea_set_one_line(preset_speed_input, true);
    lv_obj_set_style_text_align(preset_speed_input, LV_TEXT_ALIGN_CENTER, LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(preset_speed_input, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_add_event_cb(preset_speed_input, preset_input_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 加号按钮
    lv_obj_t *plus_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(plus_btn, 35, 35);
    lv_obj_set_pos(plus_btn, 385, 155);
    lv_obj_add_event_cb(plus_btn, plus_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *plus_label = lv_label_create(plus_btn);
    lv_label_set_text(plus_label, "+");
    lv_obj_set_style_text_font(plus_label, &my_font_24, LV_STATE_DEFAULT);
    lv_obj_center(plus_label);
    
    // // 当前预设值显示
    // preset_speed_label = lv_label_create(lv_scr_act());
    // lv_label_set_text(preset_speed_label, "0");
    // lv_obj_set_style_text_font(preset_speed_label, &my_font_24, LV_STATE_DEFAULT);
    // lv_obj_set_pos(preset_speed_label, 320, 200);
}

/**
 * @brief  创建速度设定滑块
 * @param  无
 * @return 无
 */
static void create_speed_slider(void)
{
    // 创建滑块
    speed_slider = lv_slider_create(lv_scr_act());
    lv_obj_set_size(speed_slider, 240, 25);
    lv_obj_set_pos(speed_slider, 220, 240);
    lv_slider_set_range(speed_slider, -3000, 3000);
    lv_slider_set_value(speed_slider, 0, LV_ANIM_OFF);
    lv_obj_add_event_cb(speed_slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    
    // // 确保滑块不显示任何文本 - 设置各部分的文本透明度为0
    // lv_obj_set_style_text_opa(speed_slider, LV_OPA_TRANSP, LV_PART_MAIN);
    // lv_obj_set_style_text_opa(speed_slider, LV_OPA_TRANSP, LV_PART_INDICATOR);
    // lv_obj_set_style_text_opa(speed_slider, LV_OPA_TRANSP, LV_PART_KNOB);
    
    // // 也可以设置文本字体大小为0作为备用方案
    // lv_obj_set_style_text_font(speed_slider, NULL, LV_PART_MAIN);
    // lv_obj_set_style_text_font(speed_slider, NULL, LV_PART_INDICATOR);
    // lv_obj_set_style_text_font(speed_slider, NULL, LV_PART_KNOB);
}

/**
 * @brief  更新电机速度显示
 * @param  speed: 当前速度
 * @return 无
 */
void update_motor_speed_display(int32_t speed)
{
    char speed_str[16];
    
    current_speed = speed;
    
    // 更新数值显示
    sprintf(speed_str, "%d", (int)speed);
    lv_label_set_text(speed_label, speed_str);
    
    // 更新仪表盘指针
    if (speed_indicator != NULL)
    {
        lv_meter_set_indicator_value(speed_meter, speed_indicator, speed);
    }
}

/**
 * @brief  检查并更新实际速度显示
 * @param  无
 * @return 无
 * @note   应在主循环中周期性调用此函数
 */
void check_and_update_actual_speed(void)
{
    // 检查是否有新的速度数据
    if (is_speed_data_updated())
    {
        int32_t actual_speed = get_actual_speed();
        
        // 更新实际速度显示
        update_motor_speed_display(actual_speed);
        
        printf("Actual Speed Updated: %d RPM\r\n", (int)actual_speed);
    }
}

/**
 * @brief  更新预设速度显示
 * @param  speed: 预设速度
 * @return 无
 */
void update_preset_speed_display(int32_t speed)
{
    char speed_str[16];
    
    sprintf(speed_str, "%d", (int)speed);
    // lv_label_set_text(preset_speed_label, speed_str);
    lv_textarea_set_text(preset_speed_input, speed_str);
    
    // 通过CAN发送目标速度到G4控制板 (ID: 0x100)
    printf("Send Target Speed: %d RPM\r\n", (int)speed);
    send_can_command(0x100, speed);
}

/**
 * @brief  显示数字键盘弹窗 (使用Flexbox布局重构)
 * @param  无
 * @return 无
 */
void show_number_keyboard_popup(void)
{
    // 如果弹窗已经存在，直接返回
    if (keyboard_popup != NULL)
    {
        return;
    }
    
    // 创建半透明背景
    keyboard_bg = lv_obj_create(lv_scr_act());
    lv_obj_set_size(keyboard_bg, scr_act_width(), scr_act_height());
    lv_obj_set_pos(keyboard_bg, 0, 0);
    lv_obj_set_style_bg_color(keyboard_bg, lv_color_black(), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(keyboard_bg, LV_OPA_50, LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(keyboard_bg, 0, LV_STATE_DEFAULT);
    lv_obj_add_flag(keyboard_bg, LV_OBJ_FLAG_FLOATING);
    
    // 创建弹窗容器
    keyboard_popup = lv_obj_create(keyboard_bg);
    lv_obj_set_size(keyboard_popup, 340, 280);
    lv_obj_center(keyboard_popup);
    lv_obj_set_style_bg_color(keyboard_popup, lv_color_white(), LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(keyboard_popup, 2, LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(keyboard_popup, lv_color_black(), LV_STATE_DEFAULT);
    lv_obj_set_style_radius(keyboard_popup, 10, LV_STATE_DEFAULT);
    
    /* --- Flexbox 布局核心修改 --- */
    // 1. 在容器 (keyboard_popup) 上启用Flexbox布局
    lv_obj_set_layout(keyboard_popup, LV_LAYOUT_FLEX);
    
    // 2. 设置布局流向为垂直列（子控件将从上到下排列）
    lv_obj_set_flex_flow(keyboard_popup, LV_FLEX_FLOW_COLUMN);

    // 3. 设置子控件在容器内的对齐方式
    //    主轴（垂直方向）: 居中对齐
    //    交叉轴（水平方向）: 居中对齐
    //    内容对齐（多行时）: 居中对齐
    lv_obj_set_flex_align(keyboard_popup, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 4. 设置Flexbox布局的行间距（即子控件之间的垂直间隙）
    lv_obj_set_style_pad_row(keyboard_popup, 5, LV_STATE_DEFAULT);
    /* --- Flexbox 布局修改结束 --- */


    // 创建输入显示区域 - 无需再手动设置位置
    keyboard_input_display = lv_textarea_create(keyboard_popup);
    lv_obj_set_size(keyboard_input_display, 320, 35);
    // lv_obj_set_pos(keyboard_input_display, 10, 10); // <-- 已被Flexbox替代，删除或注释
    lv_textarea_set_text(keyboard_input_display, "");
    lv_textarea_set_one_line(keyboard_input_display, true);
    lv_obj_set_style_text_align(keyboard_input_display, LV_TEXT_ALIGN_CENTER, LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(keyboard_input_display, &my_font_24, LV_STATE_DEFAULT);
    
    // 创建数字键盘 - 无需再手动设置位置
    keyboard = lv_keyboard_create(keyboard_popup);
    // 增加键盘高度以确保所有按键（包括+/-）都能完整显示
    lv_obj_set_size(keyboard, 320, 200); 
    // lv_obj_set_pos(keyboard, 10, 50); // <-- 已被Flexbox替代，删除或注释
    lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
    lv_keyboard_set_textarea(keyboard, keyboard_input_display);
    lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_READY, NULL);
    lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_CANCEL, NULL);
    
    // 设置当前预设速度值到输入框
    char speed_str[16];
    sprintf(speed_str, "%d", (int)preset_speed);
    lv_textarea_set_text(keyboard_input_display, speed_str);
}

/**
 * @brief  关闭数字键盘弹窗
 * @param  无
 * @return 无
 */
void close_number_keyboard_popup(void)
{
    if (keyboard_bg != NULL)
    {
        lv_obj_del(keyboard_bg);
        keyboard_bg = NULL;
        keyboard_popup = NULL;
        keyboard = NULL;
        keyboard_input_display = NULL;
    }
}

/**
 * @brief  电机控制界面初始化
 * @param  无
 * @return 无
 */
void motor_control_ui_init(void)
{
    // 设置背景色
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0xF0F0F0), LV_STATE_DEFAULT);
    
    // 创建各个UI组件
    create_speed_meter();           // 创建速度仪表盘
    create_control_buttons();       // 创建控制按钮
    create_preset_speed_control();  // 创建预设速度控制
    create_speed_slider();          // 创建速度滑块
    
    // 初始化显示
    update_motor_speed_display(0);
    update_preset_speed_display(0);
    
    printf("Motor Control UI Init Complete\r\n");
}
